package com.zzyl.nursing.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.utils.PDFUtil;
import com.zzyl.common.utils.file.FileUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.HealthAssessment;
import com.zzyl.nursing.service.IHealthAssessmentService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import static com.zzyl.common.constant.CacheConstants.CACHE_NURSING_HEALTHASSESSMENT_REPORT;



/**
 * 健康评估Controller
 * 
 * @author xw
 * @date 2025-01-06
 */
@Api(tags = "健康评估管理")
@RestController
@RequestMapping("/nursing/healthAssessment")
public class HealthAssessmentController extends BaseController
{

    @Autowired
    private IHealthAssessmentService healthAssessmentService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;



    /**
     * 查询健康评估列表
     */
    @ApiOperation("查询健康评估列表")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:list')")
    @GetMapping("/list")
    public TableDataInfo<List<HealthAssessment>> list(@ApiParam("健康评估查询条件") HealthAssessment healthAssessment)
    {
        startPage();
        List<HealthAssessment> list = healthAssessmentService.selectHealthAssessmentList(healthAssessment);
        return getDataTable(list);
    }

    /**
     * 导出健康评估列表
     */
    @ApiOperation("导出健康评估列表")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:export')")
    @Log(title = "健康评估", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@ApiParam(value = "健康评估查询条件") HttpServletResponse response, HealthAssessment healthAssessment)
    {
        List<HealthAssessment> list = healthAssessmentService.selectHealthAssessmentList(healthAssessment);
        ExcelUtil<HealthAssessment> util = new ExcelUtil<HealthAssessment>(HealthAssessment.class);
        util.exportExcel(response, list, "健康评估数据");
    }

    /**
     * 获取健康评估详细信息
     */
    @ApiOperation("获取健康评估详细信息")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:query')")
    @GetMapping(value = "/{id}")
    public R<HealthAssessment> getInfo(@ApiParam("健康评估ID") @PathVariable("id") Long id)
    {
        return R.ok(healthAssessmentService.selectHealthAssessmentById(id));
    }

    /**
     * 新增健康评估
     */
    @ApiOperation("新增健康评估")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:add')")
    @Log(title = "健康评估", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam("健康评估信息") @RequestBody HealthAssessment healthAssessment) throws IOException {
        return success(healthAssessmentService.insertHealthAssessment(healthAssessment));
    }


    /**
     * 修改健康评估
     */
    @ApiOperation("修改健康评估")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:edit')")
    @Log(title = "健康评估", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam("健康评估信息") @RequestBody HealthAssessment healthAssessment)
    {
        extracted();
        return toAjax(healthAssessmentService.updateHealthAssessment(healthAssessment));
    }

    /**
     * 删除健康评估
     */
    @ApiOperation("删除健康评估")
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:remove')")
    @Log(title = "健康评估", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam("健康评估ID数组") @PathVariable Long[] ids)
    {
        extracted();
        return toAjax(healthAssessmentService.deleteHealthAssessmentByIds(ids));
    }

    private void extracted() {
        redisTemplate.delete(CACHE_NURSING_HEALTHASSESSMENT_REPORT);
    }

    //文件上传
    @PostMapping("/upload")
    public AjaxResult uploadFile(String idCardNo,MultipartFile file) throws IOException {
            String olderFilename = file.getOriginalFilename();
            String newFileName= UUID.randomUUID().toString()
                    .replace("-", "")+olderFilename
                    .substring(olderFilename.lastIndexOf("."));
            //指定文件夹路径
            File file1 = new File("D:\\image");
            //判断文件夹是否存在
            if(!file1.exists()){
                file1.mkdirs();
            }

            // 保存文件
        File file2 = new File("D:\\image\\" + newFileName);
        file.transferTo(file2);
            //读取文件
            String s = PDFUtil.pdfToString(new FileInputStream(file2));

            //存储到redis中
            redisTemplate.opsForHash().put(CACHE_NURSING_HEALTHASSESSMENT_REPORT,idCardNo,s);


            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", file1);
            ajax.put("fileName", newFileName);
            ajax.put("newFileName", FileUtils.getName(newFileName));
            ajax.put("originalFilename", file.getOriginalFilename());
        return ajax;
    }


}
